package cn.itcast.bos.action;

import java.io.File; 
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.struts2.ServletActionContext;
import org.hibernate.criterion.DetachedCriteria;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import cn.itcast.bos.domain.PageBean;
import cn.itcast.bos.domain.Region;
import cn.itcast.bos.utils.PinYin4jUtils;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

@Controller
@Scope("prototype")
public class RegionAction extends BaseAction<Region> {

	private File myFile;
	public void setMyFile(File myFile) {
		this.myFile = myFile;
	}

	/*private int page;
	private int rows;
	public void setPage(int page) {
		this.page = page;
	}
	public void setRows(int rows) {
		this.rows = rows;
	}*/

	//选择区域
	public String findListByAjax(){
		List<Region> list = regionService.findAll();

		JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.setExcludes(new String[]{"subareas"});
		JSONArray jsonArray = JSONArray.fromObject(list, jsonConfig);
		String json = jsonArray.toString();
		
		ServletActionContext.getResponse().setContentType("text/json;charset=utf-8");
		try {
			ServletActionContext.getResponse().getWriter().print(json);
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return NONE;
	}
	
		//分页显示
		public String pageList() throws IOException{
			/*PageBean pageBean = new PageBean();
			pageBean.setCurrentPage(page);
			pageBean.setPageSize(rows);
			
			DetachedCriteria detachedCriteria = DetachedCriteria.forClass(Region.class);
			pageBean.setDetachedCriteria(detachedCriteria);*/
			regionService.pageQuery(pageBean);
			this.writePageBeanToJson(pageBean, new String[]{"detachedCriteria","currentPage","pageSize","subareas"});
			/*JsonConfig jsonConfig = new JsonConfig();
			jsonConfig.setExcludes(new String[]{"detachedCriteria","currentPage","pageSize"});
			JSONObject jsonObject = JSONObject.fromObject(pageBean, jsonConfig);
			String json = jsonObject.toString();
			ServletActionContext.getResponse().setContentType("text/json;charset=utf-8");
			ServletActionContext.getResponse().getWriter().print(json);*/
			return NONE;
		}
	
	public String update() throws FileNotFoundException, IOException{
		//System.out.println("myFile");
		String flag = "1";
		List<Region> list = new ArrayList<Region>();
		try {
				//读取Excel文件
				HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(myFile));
				HSSFSheet sheet = workbook.getSheetAt(0);
				//循环sheet页 得到每一行数据
				for (Row row : sheet) {
					int rowNum =row.getRowNum();
					if (rowNum == 0) {
						//第一行为文本标题行 忽略
						continue;
					}
					String id = row.getCell(0).getStringCellValue();
					String province = row.getCell(1).getStringCellValue();
					String city = row.getCell(2).getStringCellValue();
					String district = row.getCell(3).getStringCellValue();
					String postcode = row.getCell(4).getStringCellValue();
					//将Excel数据包封装成region对象
					Region region = new Region(id, province, city, district, postcode);
					// 使用Pinyin4J工具生成简码和城市编码
					
					// 简码----北京市北京市东城区----》BJSBJSDCQ
					String shortcode = province + city + district;
					String[] strings = PinYin4jUtils.getHeadByString(shortcode);
					shortcode = StringUtils.join(strings, "");
					
					// 城市编码----北京市-----》beijingshi
					String[] strings2 = PinYin4jUtils.stringToPinyin(city);
					String citycode = StringUtils.join(strings2);
					
					region.setShortcode(shortcode);
					region.setCitycode(citycode);
					list.add(region);
				}
				regionService.saveBatch(list);
				
				ServletActionContext.getResponse().getWriter().print(flag);
		} catch (Exception e) {
			System.out.println("....."+flag);
			e.printStackTrace();
			flag = "0";
		}
		
		return NONE;
	}
	
	
}
